package jp.co.sharp.android.parents.kidsguard.activity.edit;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.bluetooth.parentble.ParentBleTransfer;

import org.json.JSONException;
import org.json.JSONObject;

import jp.co.sharp.android.parents.kidsguard.App;
import jp.co.sharp.android.parents.kidsguard.R;
import jp.co.sharp.android.parents.kidsguard.activity.edit.dialog.SelfInfoAddressDlg;
import jp.co.sharp.android.parents.kidsguard.activity.edit.dialog.SelfInfoNoteDlg;
import jp.co.sharp.android.parents.kidsguard.activity.home.HomeActivity;
import jp.co.sharp.android.parents.kidsguard.activity.sendImage.PreviewActivity;
import jp.co.sharp.android.parents.kidsguard.activity.setting.PowerSettingActivity;
import jp.co.sharp.android.parents.kidsguard.activity.setting.SettingActivity;
import jp.co.sharp.android.parents.kidsguard.common.DealWithWaitingDlg;
import jp.co.sharp.android.parents.kidsguard.common.DelDeviceConfirmDlg;
import jp.co.sharp.android.parents.kidsguard.common.ProcessWarningDlg;
import jp.co.sharp.android.parents.kidsguard.contants.Constants;
import jp.co.sharp.android.parents.kidsguard.contants.PageSerialNumConstants;
import jp.co.sharp.android.parents.kidsguard.contants.RequestMethodConstants;
import jp.co.sharp.android.parents.kidsguard.contants.RequestReceiverConstants;
import jp.co.sharp.android.parents.kidsguard.dto.BluetoothReceiverDto;
import jp.co.sharp.android.parents.kidsguard.entity.KidInfoEntity;
import jp.co.sharp.android.parents.kidsguard.thread.BusinessThread;

public class SelfInfoActivity extends AppCompatActivity {

    private App mApp;

    private String mOldAddress;

    private String mOldNote;
    //
    private LinearLayout mAddressLl;

    private LinearLayout mNoteLl;

    private Button mSubmitBtn;

    private TextView mAddressTv;

    private TextView mNoteValueTv;

    private TextView mPhoneValueTv;

    public ParentBleTransfer mParentBleTransfer;

    private DelDeviceConfirmDlg mDelDeviceConfirmDlg;

    private Boolean mGetInfoFlag = false;

    private Boolean mOnSyncResultFlag = false;

    private DealWithWaitingDlg mDealWithWaitingDlg;

    private KidInfoEntity mKidInfoEntity;

    private boolean mScreenOperate = false;

    private boolean mUnReceiver = true;

    private static final String TAG = "TAG";
    // ページジャンプ
    private boolean mPageJumpFlag = false;
    // システムホームプレス
    private boolean mSystemHomeFlag = false;
    // プログラムロック
    private String mSyncLock = "syncLock";
    // 編集を再開しています...
    private boolean mEditReopening = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "SelfInfoActivity_onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_self_info);
        mApp = (App)getApplication();
        mAddressLl = findViewById(R.id.address);
        mNoteLl = findViewById(R.id.note);
        initListener();
        initReceiver();
        init();
    }

    @Override
    public void onBackPressed() {
        if ( (mOldAddress.equals(mAddressTv.getText().toString())) && (mOldNote.equals(mNoteValueTv.getText().toString())) ){
            sendExitToKid();
        }else {
            String message = "編集内容は保存されていません。編集内容を破棄して前の画面に戻りますか？";
            mDelDeviceConfirmDlg = new DelDeviceConfirmDlg(SelfInfoActivity.this, R.style.commonDlg, message);
            mDelDeviceConfirmDlg.show();
            mDelDeviceConfirmDlg.setOnOperateSetListener(new DelDeviceConfirmDlg.OnOperateSetListener() {
                @Override
                public void onOperateSet() {
                    sendExitToKid();
                }
            });
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "SelfInfoActivity_onStart");
        mApp.setAppStatus(Constants.APP_STATUS_INIT);
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(TAG, "SelfInfoActivity_onRestart");
        synchronized (mSyncLock) {
            if (mSystemHomeFlag) {
                if ((mApp.getAppStatus() != Constants.APP_STATUS_USER_PRESENT)&&(!mEditReopening)) {
                    String message = "編集を再開しています...";
                    mDealWithWaitingDlg = new DealWithWaitingDlg(SelfInfoActivity.this, R.style.commonDlg, message, false);
                    mDealWithWaitingDlg.show();
                    mEditReopening = true;
                    mScreenOperate = true;
                    sendIsUsingToKid();
                    // ブロードキャストの再登録
                    unregisterReceiver(systemReceiver);
                    initReceiver();
                }
                mSystemHomeFlag = false;
            }
        }
    }
    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "SelfInfoActivity_onStop");
        if (!mUnReceiver) {
            synchronized (mSyncLock) {
                try {
                    unregisterReceiver(receiver);
                } catch (IllegalArgumentException e) {
                    if (e.getMessage().contains("Receiver not registered")) {
                        // Ignore this exception. This is exactly what is desired
                        Log.e(TAG, e.getMessage());
                    } else {
                        // unexpected, re-throw
                        Log.e(TAG, e.getMessage());
                    }
                }
                if (mPageJumpFlag) {
                    try {
                        unregisterReceiver(receiver);
                    } catch (IllegalArgumentException e) {
                        if (e.getMessage().contains("Receiver not registered")) {
                            // Ignore this exception. This is exactly what is desired
                            Log.e(TAG, e.getMessage());
                        } else {
                            // unexpected, re-throw
                            Log.e(TAG, e.getMessage());
                        }
                    }
                    mPageJumpFlag = false;
                }
                mUnReceiver = true;
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "SelfInfoActivity_onDestroy");
        if (!mUnReceiver) {
            synchronized (mSyncLock) {
                try {
                    unregisterReceiver(receiver);
                } catch (IllegalArgumentException e) {
                    if (e.getMessage().contains("Receiver not registered")) {
                        // Ignore this exception. This is exactly what is desired
                        Log.e(TAG, e.getMessage());
                    } else {
                        // unexpected, re-throw
                        Log.e(TAG, e.getMessage());
                    }
                }
                if (mPageJumpFlag) {
                    try {
                        unregisterReceiver(receiver);
                    } catch (IllegalArgumentException e) {
                        if (e.getMessage().contains("Receiver not registered")) {
                            // Ignore this exception. This is exactly what is desired
                            Log.e(TAG, e.getMessage());
                        } else {
                            // unexpected, re-throw
                            Log.e(TAG, e.getMessage());
                        }
                    }
                    mPageJumpFlag = false;
                }
                mUnReceiver = true;
            }
        }
    }



    private void initListener() {
        Log.i(TAG, "SelfInfoActivity_initListener");
        mKidInfoEntity = ((App)getApplication()).findKidInfoBySelect();
        ImageView back = findViewById(R.id.back);
        mSubmitBtn = findViewById(R.id.submit);
        mSubmitBtn.setEnabled(false);
        mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_normal, null));
        mAddressTv = (TextView) findViewById(R.id.addressValue);
        mNoteValueTv = (TextView) findViewById(R.id.noteValue);
        mPhoneValueTv = (TextView) findViewById(R.id.phoneValue);
        TextView mKidNameTv = findViewById(R.id.textViewKidName);
        String kidNameDescribe = mKidInfoEntity.getKidName()+"のキッズケータイ";
        mKidNameTv.setText(kidNameDescribe);
        mAddressLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    showSelfInfoAddressDlg();
            }
        });
        mNoteLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    showSelfInfoNoteDlg();
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ( (mOldAddress.equals(mAddressTv.getText().toString())) && (mOldNote.equals(mNoteValueTv.getText().toString())) ){
                    sendExitToKid();
                }else {
                    String message = "編集内容は保存されていません。編集内容を破棄して前の画面に戻りますか？";
                    mDelDeviceConfirmDlg = new DelDeviceConfirmDlg(SelfInfoActivity.this, R.style.commonDlg, message);
                    mDelDeviceConfirmDlg.show();
                    mDelDeviceConfirmDlg.setOnOperateSetListener(new DelDeviceConfirmDlg.OnOperateSetListener() {
                        @Override
                        public void onOperateSet() {
                            sendExitToKid();
                        }
                    });
                }
            }
        });
        mSubmitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    // Dlg
                String message = "キッズケータイへ反映中..";
                mDealWithWaitingDlg = new DealWithWaitingDlg(SelfInfoActivity.this, R.style.commonDlg, message, true);
                mDealWithWaitingDlg.show();
                submit();
            }
        });
    }

    private void back() {
        Log.i(TAG, "SelfInfoActivity_back");
        mPageJumpFlag = true;
        Intent intent = new Intent();
        intent.setClass(this, EditActivity.class);
        startActivity(intent);
    }

    private void submit() {
        Log.i(TAG, "SelfInfoActivity_submit");
        try {
            JSONObject submit = new JSONObject();
            submit.put("method","setProfile");
            JSONObject data = new JSONObject();
            String phone = mPhoneValueTv.getText().toString();
            if (phone.isEmpty()){
                data.put("phone",JSONObject.NULL);
            }else {
                data.put("phone",phone);
            }
            String note = mNoteValueTv.getText().toString();
            if (note.isEmpty()){
                data.put("memo",JSONObject.NULL);
            }else {
                data.put("memo",note);
            }
            String address = mAddressTv.getText().toString();
            if (address.isEmpty()){
                data.put("address",JSONObject.NULL);
            }else {
                data.put("address",address);
            }
            submit.put("data",data);
//            mParentBleTransfer.writeLeData(macAddress, submit.toString());
            new BusinessThread(Constants.SEND_KID_SET_INFO,
                    mParentBleTransfer,
                    mKidInfoEntity,
                    submit,
                    PageSerialNumConstants.PAGE_SERIAL_SELF_INFO).start();
        } catch (JSONException e) {
             Log.e(TAG,e.getMessage());
        }
    }

    private void initReceiver() {
        Log.i(TAG, "SelfInfoActivity_initReceiver");
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        // ホームボタン
        filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        // Bluetooth
        filter.addAction(RequestReceiverConstants.RECEIVER_BLUETOOTH_CONNECT_STATE_CHANGED);
        //kid返回数据
        filter.addAction(RequestReceiverConstants.RECEIVER_BLUETOOTH_LISTENER_DATA_CHANGED);
        //isFinish
        filter.addAction(RequestReceiverConstants.RECEIVER_BLUETOOTH_WRITE_DATA_CHANGED);
        registerReceiver(receiver, filter);
        registerReceiver(systemReceiver, filter);
        this.mParentBleTransfer = ((App) getApplication()).getmParentBleTransfer();
        mUnReceiver = false;
    }

    /**
     * システム放送
     */
    private final BroadcastReceiver systemReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                String action = intent.getAction();
                if (Intent.ACTION_SCREEN_OFF.equals(action)) {
                    Log.i(TAG, "SelfInfoActivity_systemReceiver_ACTION_SCREEN_OFF");
                    synchronized (mSyncLock) {
                        mApp.setAppStatus(Constants.APP_STATUS_SCREEN_OFF);
                        mScreenOperate = true;
                        sendExitToKid();
                    }
                } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
                    Log.i(TAG, "SelfInfoActivity_systemReceiver_ACTION_USER_PRESENT");
                    synchronized (mSyncLock) {
                        mApp.setAppStatus(Constants.APP_STATUS_USER_PRESENT);
                        if (!mPageJumpFlag) {
                            String message = "編集を再開しています...";
                            mDealWithWaitingDlg = new DealWithWaitingDlg(SelfInfoActivity.this, R.style.commonDlg, message, false);
                            mDealWithWaitingDlg.show();
                            mEditReopening = true;
                            mScreenOperate = true;
                            sendIsUsingToKid();
                            // ブロードキャストの再登録
                            unregisterReceiver(systemReceiver);
                            initReceiver();
                        }
                    }
                } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
                    Log.i(TAG, "SelfInfoActivity_systemReceiver_ACTION_CLOSE_SYSTEM_DIALOGS");
                    synchronized (mSyncLock) {
                        mApp.setAppStatus(Constants.APP_STATUS_CLOSE_SYSTEM_DIALOGS);
                        mSystemHomeFlag = true;
                        mScreenOperate = true;
                        sendExitToKid();
                    }
                }else if (RequestReceiverConstants.RECEIVER_BLUETOOTH_CONNECT_STATE_CHANGED.equals(action)) {
                    Log.i(TAG, "SelfInfoActivity_receiver_RECEIVER_BLUETOOTH_CONNECT_STATE_CHANGED");
                    BluetoothReceiverDto bluetoothReceiverDto = (BluetoothReceiverDto) intent.getSerializableExtra(RequestReceiverConstants.RECEIVER_KEY);
                    String macAddress = bluetoothReceiverDto.getAddress();
                    if (((App)getApplication()).isMimamoriCheck(macAddress)) {
                        ((App)getApplication()).addmLsIsMimamori(macAddress);
                        sendExitToKid();
                        mPageJumpFlag = true;
                        Intent intent1 = new Intent();
                        intent1.setAction(Constants.ACTION_IS_MIMAMORI);
                        intent1.setClass(SelfInfoActivity.this, HomeActivity.class);
                        startActivity(intent1);
                    } else {
                        mKidInfoEntity.setBtConnStatus(Constants.DEVICE_DISCONNECTION_SUCCESS);
                        ((App)getApplication()).updateKidInfo(mKidInfoEntity);

                        String message = mKidInfoEntity.getKidName() + "と切断されました。編集を中断します。";
                        ProcessWarningDlg processWarringDlg = new ProcessWarningDlg(SelfInfoActivity.this, R.style.commonDlg, message);
                        processWarringDlg.show();
                        processWarringDlg.setOnOperateSetListener(new ProcessWarningDlg.OnOperateSetListener() {
                            @Override
                            public void onOperateSet() {
                                processWarringDlg.dismiss();
                                mPageJumpFlag = true;
                                Intent intent1 = new Intent();
                                intent1.setClass(SelfInfoActivity.this, SettingActivity.class);
                                startActivity(intent1);
                            }
                        });
                    }
                }
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }
    };

    /**
     * Dialog閉じる
     */
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (null != mDealWithWaitingDlg && mDealWithWaitingDlg.isShowing()) {
                mDealWithWaitingDlg.dismiss();
            }
        }
    };


    private void init() {
        Log.i(TAG, "SelfInfoActivity_init");
        // Dlg
        String message = "キッズケータイから情報を取得しています...";
        mDealWithWaitingDlg = new DealWithWaitingDlg(SelfInfoActivity.this, R.style.commonDlg, message, true);
        mDealWithWaitingDlg.show();
        //is using
        sendIsUsingToKid();

    }


    /**
     * BroadcastReceiver
     */
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
             if (RequestReceiverConstants.RECEIVER_BLUETOOTH_LISTENER_DATA_CHANGED.equals(action)) {
                Log.i(TAG, "SelfInfoActivity_receiver_RECEIVER_BLUETOOTH_LISTENER_DATA_CHANGED");
                BluetoothReceiverDto bluetoothReceiverDto = (BluetoothReceiverDto) intent.getSerializableExtra(RequestReceiverConstants.RECEIVER_KEY);
                try {
                    JSONObject resp = new JSONObject(bluetoothReceiverDto.getRetStr());
                    String method = resp.getString("method");

                    if (null != method) {
                        if (RequestMethodConstants.METHOD_GET_PROFILE.equals(method)) {
                            JSONObject data = resp.getJSONObject("data");
                            Log.i(TAG, "SelfInfoActivity_receiver_RECEIVER_BLUETOOTH_LISTENER_DATA_CHANGED_getProfile:"+data.toString());
                            mOldAddress = data.isNull("address")?"":data.getString("address");
                            mOldNote = data.isNull("memo")?"":data.getString("memo");
                            mAddressTv.setText(data.isNull("address")?"":data.getString("address"));
                            mNoteValueTv.setText(data.isNull("memo")?"":data.getString("memo"));
                            mPhoneValueTv.setText(data.isNull("phone")?"":data.getString("phone"));
                            handler.sendEmptyMessage(0);
                        } else if (RequestMethodConstants.METHOD_GET_OPERATING_STATE.equals(method)) {
                            Log.i(TAG, "SelfInfoActivity_receiver_RECEIVER_BLUETOOTH_LISTENER_DATA_CHANGED_getOperatingState");
                            JSONObject data = resp.getJSONObject("data");
                            if (Constants.IS_USING_OK.equals(data.getString("state"))) {
                                Log.i(TAG, "SelfInfoActivity_receiver_RECEIVER_BLUETOOTH_LISTENER_DATA_CHANGED_getOperatingState_IS_USING_OK");
                                sendEnterToKid();
                            }else {
                                Log.i(TAG, "SelfInfoActivity_receiver_RECEIVER_BLUETOOTH_LISTENER_DATA_CHANGED_getOperatingState_!IS_USING_OK");
                                handler.sendEmptyMessage(0);
                                String message = "別のスマートフォンから操作中のため、設定を変更できません。";
                                ProcessWarningDlg processWarringDlg = new ProcessWarningDlg(SelfInfoActivity.this, R.style.commonDlg, message);
                                processWarringDlg.show();
                                processWarringDlg.setOnOperateSetListener(new ProcessWarningDlg.OnOperateSetListener() {
                                    @Override
                                    public void onOperateSet() {
                                        processWarringDlg.dismiss();
                                        // ページジャンプ
                                        mPageJumpFlag = true;
                                        SelfInfoActivity.this.finish();
                                    }
                                });
                            }
                        } else if (RequestMethodConstants.METHOD_ON_SYNC_RESULT.equals(method)) {
                            if (Constants.ON_SYNC_RESULT.equals(resp.getString("result"))) {
                                Log.i(TAG, "SelfInfoActivity_receiver_RECEIVER_BLUETOOTH_LISTENER_DATA_CHANGED_onSyncResult_ON_SYNC_RESULT");
                                sendExitToKid();
                                handler.sendEmptyMessage(0);
                            }else {
                                handler.sendEmptyMessage(0);
                                String message = "キッズケータイへ反映できませんでした";
                                ProcessWarningDlg processWarringDlg = new ProcessWarningDlg(SelfInfoActivity.this, R.style.commonDlg, message);
                                processWarringDlg.show();
                                processWarringDlg.setOnOperateSetListener(new ProcessWarningDlg.OnOperateSetListener() {
                                    @Override
                                    public void onOperateSet() {
                                        processWarringDlg.dismiss();
                                    }
                                });
                            }
                        }else if (RequestMethodConstants.METHOD_ON_OPERATE_INTERRUPT.equals(method)){
                            Log.i(TAG, "SelfInfoActivity_receiver_RECEIVER_BLUETOOTH_LISTENER_DATA_CHANGED_onOperateInterrupt");
                            String message = "キッズケータイが操作されました。編集を中断します。";
                            ProcessWarningDlg processWarringDlg = new ProcessWarningDlg(SelfInfoActivity.this, R.style.commonDlg, message);
                            processWarringDlg.show();
                            processWarringDlg.setOnOperateSetListener(new ProcessWarningDlg.OnOperateSetListener() {
                                @Override
                                public void onOperateSet() {
                                    processWarringDlg.dismiss();
                                    back();
                                }
                            });
                        }
                    }
                } catch (JSONException e) {
                     Log.e(TAG,e.getMessage());
                }

            } else if (RequestReceiverConstants.RECEIVER_BLUETOOTH_WRITE_DATA_CHANGED.equals(action)) {
                Log.i(TAG, "SelfInfoActivity_receiver_RECEIVER_BLUETOOTH_WRITE_DATA_CHANGED");
                //isFinish
                BluetoothReceiverDto bluetoothReceiverDto = (BluetoothReceiverDto) intent.getSerializableExtra(RequestReceiverConstants.RECEIVER_KEY);
                if (bluetoothReceiverDto.isFinished()) {
                    if (mGetInfoFlag) {
                        mGetInfoFlag = false;
                        if (!mScreenOperate){
                            sendGetProfileToKid();
                        }else {
                            mEditReopening = false;
                            mScreenOperate = false;
                            handler.sendEmptyMessage(0);
                        }
                    }
                    if (mOnSyncResultFlag) {
                        mOnSyncResultFlag = false;
                        if (!mScreenOperate){
                            back();
                        }
                    }
                }

            }
        }
    };
    /**
     * is using
     */
    public void sendIsUsingToKid(){
        Log.i(TAG, "SelfInfoActivity_sendIsUsingToKid");
        try{
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("method","getOperatingState");
            jsonObject.put("data",JSONObject.NULL);
            new BusinessThread(Constants.SEND_KID_IS_USING,
                    mParentBleTransfer,
                    mKidInfoEntity,
                    jsonObject,
                    PageSerialNumConstants.PAGE_SERIAL_SELF_INFO).start();
        }catch (JSONException e){
             Log.e(TAG,e.getMessage());
        }catch (Exception e){
             Log.e(TAG,e.getMessage());
        }
    }
    /**
     * getInfo
     */
    public void sendGetProfileToKid(){
        Log.i(TAG, "SelfInfoActivity_sendGetProfileToKid");
        try{
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("method","getProfile");
            jsonObject.put("data",JSONObject.NULL);
            new BusinessThread(Constants.SEND_KID_GET_INFO,
                    mParentBleTransfer,
                    mKidInfoEntity,
                    jsonObject,
                    PageSerialNumConstants.PAGE_SERIAL_SELF_INFO).start();
        }catch (JSONException e){
             Log.e(TAG,e.getMessage());
        }catch (Exception e){
             Log.e(TAG,e.getMessage());
        }
    }
    /**
     * enter
     */
    public void sendEnterToKid(){
        Log.i(TAG, "SelfInfoActivity_sendEnterToKid");
        mGetInfoFlag = true;
        new BusinessThread(Constants.SEND_KID_ENTER,
                mParentBleTransfer,
                mKidInfoEntity,
                null,
                PageSerialNumConstants.PAGE_SERIAL_SELF_INFO).start();
    }
    /**
     * exit
     */
    public void sendExitToKid(){
        Log.i(TAG, "SelfInfoActivity_sendExitToKid");
        mOnSyncResultFlag = true;
        new BusinessThread(Constants.SEND_KID_EXIT,
                            mParentBleTransfer,
                            mKidInfoEntity,
                            null,
                            PageSerialNumConstants.PAGE_SERIAL_SELF_INFO).start();

    }
    public void showSelfInfoAddressDlg() {
        Log.i(TAG, "SelfInfoActivity_showSelfInfoAddressDlg");
        String address = mAddressTv.getText().toString();
        SelfInfoAddressDlg selfInfoAddressDlg = new SelfInfoAddressDlg(this, R.style.dateTimePicker, address);
        selfInfoAddressDlg.setOnAddressSetListener(new SelfInfoAddressDlg.OnAddressSetListener() {

            @Override
            public void onAddressSet(String address) {
                mAddressTv.setText(address);
                if (!(mOldAddress.equals(address))){
                    mSubmitBtn.setEnabled(true);
                    mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_change, null));
                }else {
                    mSubmitBtn.setEnabled(false);
                    mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_normal, null));
                }
            }
        });
        selfInfoAddressDlg.show();
    }
    public void showSelfInfoNoteDlg() {
        Log.i(TAG, "SelfInfoActivity_showSelfInfoNoteDlg");
        String note = mNoteValueTv.getText().toString();
        SelfInfoNoteDlg selfInfoNoteDlg = new SelfInfoNoteDlg(this, R.style.dateTimePicker, note);
        selfInfoNoteDlg.setOnNoteSetListener(new SelfInfoNoteDlg.OnNoteSetListener() {
            @Override
            public void onNoteSet(String note) {
                mNoteValueTv.setText(note);
                if (!(mOldNote.equals(note))){
                    mSubmitBtn.setEnabled(true);
                    mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_change, null));
                }else {
                    mSubmitBtn.setEnabled(false);
                    mSubmitBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.button_reflect_normal, null));
                }
            }

        });
        selfInfoNoteDlg.show();
    }



}
